﻿<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Areas/CustomerAdmin/Customer.Master" CodeBehind="Rules.aspx.cs" Inherits="OnlineBusinesses.Areas.CustomerAdmin.Rules" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="ContentCustomerAdmin">
    <div class="col-md-12 col-lg-12 col-xs-12 col-sm-12" style="padding: 10px;">
        <h4 style="font-weight: bold;">Phân quyền tài khoản</h4>
        <div class="col-md-6 col-lg-6 col-xs-12 col-sm-12" style="padding: 0px;">
            <div class="col-md-12 col-lg-12 col-xs-12 col-sm-12" style="padding: 0px;">
                <h4><asp:Label runat="server" ID="lblObject"></asp:Label>
                </h4>
            </div>
        </div>
        <%--<script type="text/javascript" src="../../scripts/jquery-1.7.1.min.js"></script>--%>
        <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxpanel.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxtree.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxcheckbox.js"></script>

        <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />

        <script>
            jQuery(function () {
                function getQueryStrings() {
                    var assoc = {};
                    var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
                    var queryString = location.search.substring(1);
                    var keyValues = queryString.split('&');

                    for (var i in keyValues) {
                        var key = keyValues[i].split('=');
                        if (key.length > 1) {
                            assoc[decode(key[0])] = decode(key[1]);
                        }
                    }

                    return assoc;
                }
                var qs = getQueryStrings();
                var departmentid = qs["did"];
                var accountid = qs["aid"];
                if (accountid == undefined) accountid = 0;
                if (departmentid == undefined) departmentid = 0;
                loadCustomer();
                function loadCustomer() {
                    var url = "Rules.aspx/LoadFunction";
                    $.ajax({
                        type: "POST",
                        url: url,
                        data: "{parentid: 0, departmentid: "+departmentid+", accountid: "+accountid+"}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d != undefined) {
                                var html = "<ul>";
                                $.each(data.d, function (i, item) {
                                    if (item.FunctionName != undefined) {
                                        if (item.IsPermit) html += "<li class='selected' item-value='" + item.FunctionID + "'>" + item.FunctionName;
                                        else html += "<li  item-value='" + item.FunctionID + "'>" + item.FunctionName;
                                        //alert(item.departmentID);item-checked='false' item-checked='true'
                                        if (item.HasChild > 0)
                                            html += getChild(item.FunctionID);

                                        html += "</li>";
                                    }
                                });
                                html += "</ul>";

                                $("#jqxTree").append(html);
                                $('#jqxTree').jqxTree({ height: '400px', width: '330px', hasThreeStates: true, checkboxes: true });
                                $("#jqxTree").jqxTree('checkItem', $(".selected")[0], true);
                            }
                        }
                    });
                }
                function getChild(parentid) {
                    var html = "";
                    var url = "Rules.aspx/LoadFunction";
                    $.ajax({
                        async: false,
                        global: false,
                        type: "POST",
                        url: url,
                        data: "{parentid: " + parentid + ", departmentid: " + departmentid + ", accountid: " + accountid + "}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            if (data.d != undefined) {
                                html = "<ul>";
                                $.each(data.d, function (i, item) {
                                    if (item.FunctionName != undefined) {
                                        if (item.IsPermit) html += "<li class='selected' item-value='" + item.FunctionID + "'>" + item.FunctionName;
                                        else html += "<li  item-value='" + item.FunctionID + "'>" + item.FunctionName;
                                        //alert(item.departmentID);item-checked='false' item-checked='true'
                                        if (item.HasChild > 0)
                                            html += getChild(item.FunctionID);

                                        html += "</li>";
                                    }
                                });
                                html += "</ul>";
                                //alert(html);
                            }
                        }
                    });
                    return html;
                }

                $(document).on('click', '#btnConfirm', function (event) {
                    var items = $('#jqxTree').jqxTree('getCheckedItems');
                    var ids = "";
                    $.each(items, function (i, item) {
                        ids += item.value + ",";
                    });
                    //alert(ids);
                    var url = "Rules.aspx/Confirm";
                    $.ajax({
                        async: false,
                        global: false,
                        type: "POST",
                        url: url,
                        data: "{departmentid: " + departmentid + ", accountid: " + accountid + ", ids:'"+ids+"'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) { }
                    });
                });
            });
        </script>

        <div class="col-md-6 col-lg-6 col-xs-12 col-sm-12" style="padding: 10px;">
            <div id='content' class="col-md-12 col-lg-12 col-xs-12 col-sm-12" style="padding: 15px;">
                <div id='jqxTree'>
                </div>
            </div>
        <div class="col-md-12 col-lg-12 col-xs-12 col-sm-12" style="padding: 20px 0px; text-align: right;">
            <span id="btnConfirm" class="btn btn-default" style="border-radius: 0px;">Xác nhận phân quyền</span>
        </div>
        </div>
    </div>
</asp:Content>
